<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0,user-scalable=no">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>Document</title>
    <link rel="stylesheet" href="../../css/api.css">
    <link rel="stylesheet" href="../../css/news.css">
    <style>
        html,
        body {
            background: #000;
        }
        .suoxiao{
            position: fixed;
            left: 10px;
            top: 30px;
            height: 30px;
            width: 30px;
            z-index: 9999;
        }

    </style>
</head>

<body>

<div id="view" v-cloak>

</div>
</body>
<script src="../../script/api.js"></script>
<script src="../../script/jquery.js"></script>
<script src="../../script/ffkj.js"></script>
<script src="../../script/vue.js"></script>
<script src="../../script/audio.js"></script>
<script src="../../script/mhsdk.js"></script>
<script src="../../script/chat.js"></script>
<script>
    var view = new Vue({
        el: '#view',
        data: {
            type: 0, // 连接情况 -1发起 0 未连接 1已连接
            status: 0, // 后端是否有推送状态过来
            imgurl: imgurl,
            ffList: [],
            ffInfo: {},
            time: '0',
            types: 2, // 1语音|2视频
            myuserid: 0,
            touser_sex : '',
            touserid: 0,
            order: '',
            roomId: null,
            winName: '',
			connectStatus:-1
        },
        methods: {
            _url(param, url) {
                _url(param, url);
            },
            // 返回图片路径
            returnImg(url) {
                return returnImg(url);
            },
        }
    })
    var audio;
    var localVideoBig = 0;
    apiready = function () {
        api.setKeepScreenOn({
            keepOn: true
        });
        _listener('keyback', function () {
        })
        if (!$api.getStorage('rtcid')) {
            _alert('缺少视频通话配置，无法进行视频通话', function () {
                api.closeWin();
            });
            return;
        }
       
        //sdk初始化
        mhsdkApi.mhInit();
        //设置用户远端视频可见回调
        mhsdkApi.setOnUserVideoAvaialeListener(function (ret) {
            console.log('vvvvvvvvvvvvvvvvvv=====================');
            console.log(JSON.stringify(ret));
            console.log('--onvideo->', ret);
            $api.setStorage('leaveTime', new Date().getTime())
            let data = ret.detail
            if (data.code == '1') { //连麦
                console.log("连麦了111")
                //showCallTime();
                mhsdkApi.startLink({
                    linkUserId: data.msg,
                    linkStreamId: data.msg,
                    playUrl: "",
                    localVideoBig: 0
                })
                if(view.type == 1){
                    api.setFrameAttr({
                        name: 'frame0/videochat_menu',
                        rect: {
                            x: 0,                 //左上角x坐标
                            y: 500,                 //左上角y坐标
                            w: 'auto',
                            h: 'auto',
                        }
                    });
                }
            }
        });
        console.log("aaaa", JSON.stringify(api.pageParam))


        //  需将参数传给videochat_menu
        view.myuserid = $api.getStorage('userid');
        view.touserid = api.pageParam['id'];
        view.type = api.pageParam['type'];
        if (api.pageParam['orderid']) {
            // 发起方推过来的订单id
            view.order = api.pageParam['orderid'];
        }
        if (api.pageParam['winName']) {
            view.winName = api.pageParam['winName'];
        }
        if (api.pageParam['roomId']) {
            view.roomId = api.pageParam['roomId'];
        }
        showDetail('frame0/videochat_menu', {
            winName: api.pageParam['winName'],
            type: view.type,
            id: view.touserid,
            orderid: view.order,
            intimate: api.pageParam['intimate'] || 0, // 是否需要限制亲密度 1需要 0不需要
        });
        if(view.type != 1){
            audio = new Audio();
            audio.play({
                url: 'widget://res/tip.mp3',
                loop: 1,
            })
            audio.normal();
        }else{
            setTimeout(function(){
				api.setFrameAttr({
				    name: 'frame0/videochat_menu',
				    rect: {
				        x: 0,                 //左上角x坐标
				        y: 500,                 //左上角y坐标
				        w: 'auto',
				        h: 'auto',
				    }
				});
			},1000)
        }
         _getUser(function(ret) {
            view.ffInfo = ret.result;
            view.touser_sex = ret.result.sex
        }, view.touserid, 1)

        _listener('returnRequest', function () {
            if (audio) {
                audio.stop();
            }
        })
        getPermission(['microphone', 'camera', 'photos'], function (code) {
            if (code != 0) {
                api.execScript({
                    frameName: 'frame0/videochat_menu',
                    script: 'refuse()'
                })
                _msg('请先开启相关权限');
            } else {

               
            }
        })
		
		_listener('sonPageParam', function(data){
		    view.connectStatus = data.value.status;
			// console.error(JSON.stringify( data ))
		})
    }

    // 加入
    function _join(uid) {
        console.log('加入房间');
        view.roomId = uid;

        console.log('订单id111',view.order)
        _ajax('api/Tencent/userSig', function (ret, err) {
            if (ret && ret.code == 200) {
                mhsdkApi.startPreview()
                mhsdkApi.enterRoom({
                    roomId: uid,
                    userId: view.myuserid,
                    userSig: ret.data.userSig,
                    streamId: "",
                    localViewBig: 0
                })
                if(view.type == 1){
                    mhsdkApi.startLink($api.getStorage('frame0/videochatlianmai'))
                }
                view.type = 1
                $api.setStorage('callStatus', 1);
                // mhsdkApi.setAudioRoute({arg:1})


                // api.bringFrameToFront({
                //     from: 'frame0/videochat_menu',
                // });
            } else {
                _leave(); // 不在通话中
                _msg(ret.msg);
                timerWin();
            }
        }, {
            user_id: view.myuserid
        })

    }

    // 离开
    function _leave() {
        $api.setStorage('callStatus', 0); // 不在通话中
        stopAudio()
        mhsdkApi.exitRoom();
        mhsdkApi.stopPreview();
        timerWin();
        api.closeWin();
        uzModuleMeiHu.closeAll()
        uzModuleMeiHu.removeFloatView()
    }

    function openMeiyan() {
        mhsdkApi.openMeiYan();
    }

    // 切换摄像头
    function switchCamera() {
        mhsdkApi.switchCamera();
    }

    function stopAudio() {
        if (audio) {
            audio.stop();
        }
        // if (rtc) {
        //     rtc.startLocalAudio();
        // }
        api.bringFrameToFront({
            from: 'frame0/videochat_menu',
        });
    }

    function setOrderId(order) {
        console.log("setOrder2",view.order)
        view.order = order
    }

    function showFloatView() {
        $api.setStorage('frame0/videochat', {
            id: view.touserid,
            type: 1,
            orderid: view.order,
            roomId: view.roomId
        });
        uzModuleMeiHu.showFloatView({
            floatView: {
                width: 400,
                height: 600,
                startX: 10,
                startY: 20,
                moveAble: true,
                showForward: true,
                showClose: false
            },
            linkView: {
                width: 240,
                cellHeight: 360,
                topMargin: 5,
                rightMargin: 5,
                bottomMargin: 5
            }
        })

        api.closeWin()


    }

</script>

</html>
